var myChart=null; var Option=null;

//获取数据,调用公用方法展示图表
$(function(){
	
	var echart = new BasicEchart('basicchartid','/user/sexcount','bar','性别分布');
    echart.init();
    
    var pieechart = new PieEchart('piechartid','/user/sexcount','性别分布');
    pieechart.init();
    
    var gaugeechart = new GaugeEchart('gaugechartid','/user/sexratio','男性占比');
    gaugeechart.init();
	 
    $.ajax({
	    	type: "POST",
			url: "/user/deptsexcount",
			async:false,
			data:{},
			dataType: "json",
			success: function(data){
				initMultipleBasic('科室性别分布','bar');
				xAxisData(data);
				seriesDatamultiple(data);
				loadChart("multiplechartid");
			}
			
		});
});
//初始化一对多图表(柱状和折线)
initMultipleBasic=function(text,type){
	Option=null;
	// 基于准备好的dom，初始化echarts实例
   // document.getElementById('mychartid').style.display="block";
     Option = {
    	    title: {
    	        text: text
    	    },
    	    tooltip: {
    	    },
    	    toolbox: {
    	        show: true,
    	        orient: 'vertical',
    	        left: 'right',
    	        top: 'center',
    	        feature: {
    	            mark: {show: true},
    	            dataView: {show: true, readOnly: false},
    	            magicType: {show: true, type: ['line', 'bar']},
    	            restore: {show: true},
    	            saveAsImage: {show: true}
    	        }
    	    },
    	    legend: {
    	        data:['男','女']
    	    },
    	    xAxis:  {
    	    },
    	    yAxis: {
    	        
    	    },
    	    series: [
    	    	{
    	            name: '男',
    	            type: type,
    	            barGap: 0,
    	        },
    	        {
    	            name: '女',
    	            type: type,
    	        }
    	    ]
    	};
}
//填充柱状折线图表数据,一对多
seriesDatamultiple=function(result){
			if (result) {
				Option.series[0].data=[];
				Option.series[1].data=[];
		        for(var i=0;i<result.length;i++){
		        	Option.series[0].data.push(result[i].boy);
		        	Option.series[1].data.push(result[i].girl);
		        }
			}

}
//--------------------------公共方法-------------------------------
 //填充x轴(柱状和折线)
 xAxisData=function(result){
		if (result) {
			Option.xAxis.data=[];
	        for(var i=0;i<result.length;i++){
	        	Option.xAxis.data.push(result[i].data);
	        }
		}
	}

 //加载
loadChart=function(chartid){
	      // 指定图表的配置项和数据
	      myChart = echarts.init(document.getElementById(chartid));
	      myChart.clear();
	      myChart.setOption(Option, true); 
	        // 使用刚指定的配置项和数据显示图表。
}
